//
//  CHAVAudioPlayer.swift
//  nurseApp
//
//  Created by cyptt on 2022/8/10.
//

import UIKit
import AVFoundation
public class CHAVAudioPlayer: NSObject,AVAudioPlayerDelegate {

    
    private   var avAudioPlay:AVAudioPlayer?
    
    private   var currentTime:TimeInterval = 0
    private  let palyBackDelay:TimeInterval = 1
    
    private var audioPlayerDecodeErrorDidOccurBlock:((_ player: AVAudioPlayer, _ error: Error?) -> ())?
    private var audioPlayerDidFinishPlayingBlock:((_ player: AVAudioPlayer, _ flag: Bool) -> ())?
    private var audioPlayerBeginInterruptionBlock:((_ player: AVAudioPlayer) -> ())?
    private  var audioPlayerEndInterruptionBlock:((_ player: AVAudioPlayer, _ flags: Int) -> ())?
    
    public init(data:Data){
        super.init()
        do{
            avAudioPlay = try? AVAudioPlayer.init(data: data)
            avAudioPlay?.delegate = self
        } catch {
            
        }
    }
    //解码错误
    public func audioPlayerDecodeErrorDidOccurB(clouse:@escaping (_ player: AVAudioPlayer, _ error: Error?) -> ()){
        self.audioPlayerDecodeErrorDidOccurBlock = clouse
    }
    
    //播放结束
    public func audioPlayerDidFinishPlayingB(clouse:@escaping (_ player: AVAudioPlayer, _ flag: Bool) -> ()){
        self.audioPlayerDidFinishPlayingBlock = clouse
    }
    
    //中断
    public  func audioPlayerBeginInterruptionB(clouse:@escaping (_ player: AVAudioPlayer) -> ()){
        self.audioPlayerBeginInterruptionBlock = clouse
    }
    
    //中断结束
    public func audioPlayerEndInterruptionB(clouse:@escaping (_ player: AVAudioPlayer, _ flags: Int) -> ()){
        self.audioPlayerEndInterruptionBlock = clouse
    }
    public init(url:URL?){
        super.init()
        var palyData:Data?
        do{
            
            if url != nil{
                palyData = try Data.init(contentsOf: url!)
            }
          
            if palyData != nil{
                avAudioPlay = try? AVAudioPlayer.init(data: palyData!)
                avAudioPlay?.delegate = self
            }

        } catch {
            
        }
        
    }
    
    /*
     开始
     */
    public func play(){

       avAudioPlay?.play()
    }
    
    /*
     暂停
     */
    public func pause(){
        self.currentTime = avAudioPlay!.currentTime
        if avAudioPlay!.isPlaying {
            avAudioPlay?.pause()
        }
       
    }
    
    public func play(atTime: TimeInterval){
        avAudioPlay?.play(atTime: atTime)
    }
    
    /*
     停止
     */
    public   func stop(){
        avAudioPlay?.stop()
        avAudioPlay = nil
    }
}


//--AVAudioPlayerDelegate
extension CHAVAudioPlayer{
    
    public func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
        print("解码错误")
        if self.audioPlayerDecodeErrorDidOccurBlock != nil{
            self.audioPlayerDecodeErrorDidOccurBlock!(player,error)
        }
    }
    
    public func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        print("播放结束")
        if self.audioPlayerDidFinishPlayingBlock != nil{
            self.audioPlayerDidFinishPlayingBlock!(player,flag)
        }
    }
    
    //中断
    public func audioPlayerBeginInterruption(_ player: AVAudioPlayer) {
        print("中断")
        if self.audioPlayerBeginInterruptionBlock != nil{
            self.audioPlayerBeginInterruptionBlock!(player)
        }
    }
    
    //终端结束
    public  func audioPlayerEndInterruption(_ player: AVAudioPlayer, withOptions flags: Int) {
        print("中断结束")
        if self.audioPlayerEndInterruptionBlock != nil{
            self.audioPlayerEndInterruptionBlock!(player, flags)
        }
    }
}
